package com.css.fxfzmh.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @Author wenyugang
 * @Date 2020/4/30
 * @Version 1.0.0
 * @Description
 */
@Component
@Slf4j
public class PlatformDownloadUtils {


    public  void download(String filePath,String filename) throws Exception {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        // 设置信息给客户端不解析
        String type = new MimetypesFileTypeMap().getContentType(filename);
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        response.setHeader("Content-type",type);
        // 设置编码
        String fileName = new String(filename.getBytes("utf-8"), "iso-8859-1");
        // 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 发送给客户端的数据
        OutputStream outputStream = response.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        InputStream is = null;
        try{
            is = this.getClass().getResourceAsStream("/"+filePath+"/" + filename);
            // 读取filename
            bis = new BufferedInputStream(is);
            int i = bis.read(buff);
            while (i != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                i = bis.read(buff);
            }
        }catch (Exception e){
            log.error("文件下载失败！",e);
            throw e;
        }finally {
            bis.close();
            is.close();
        }

    }

    public static void exportExcel(Workbook workbook, String fileName, HttpServletResponse response)  {
        if (workbook != null) {
            try {
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                String userAgent = request.getHeader("sys-agent");
                if (userAgent != null && userAgent.indexOf("Firefox") < 0 ){
                    fileName = URLEncoder.encode(fileName, "UTF8");
                }
                // 设置响应输出的头类型及下载文件的默认名称
                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                log.error("导出xls失败！",e);
            }
        }
    }
}
